﻿using System.Windows;
using System.Windows.Controls;
using LSA.Framework;
using Visuals.ViewModels;

namespace Visuals.Graph
{
    /// <summary>
    /// Interaction logic for WordsGraphControl.xaml
    /// </summary>
    public partial class WordsGraphControl : UserControl
    {
        private WordsGraphViewModel vm;

        public WordsGraphViewModel ViewModel
        {
            get { return vm; }
            set
            {
                vm = value;
                DataContext = vm;
            }
        }

        public WordsGraphControl(WordsGraphViewModel viewModel)
        {
            ViewModel = viewModel;

            InitializeComponent();
        }

        public WordsGraphControl()
        {
            InitializeComponent();
        }

        // TODO: refactor
        private void RemoveWordFromGraph(object sender, RoutedEventArgs e)
        {
            var word = (WordInfo) ((MenuItem) e.Source).DataContext;
            vm.Graph.RemoveVertex(word);
            vm.Graph.RemoveEdgeIf(edge => (word.Id == edge.Source.Id || word.Id == edge.Target.Id));
        }

        private void OpenSearchWindow(object sender, RoutedEventArgs e)
        {
            var word = (WordInfo) ((MenuItem) e.Source).DataContext;
            MainWindow.Object.OpenNewSearchPreview(word.Word); // TODO: callback
        }
    }
}
